package cn.iotab.iotkit.transport.api.access.handler.session;

import cn.iotab.iotkit.transport.api.common.message.msg.up.SessionInfo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@ToString
public class Session {

    @NotNull
    private final String sessionId;
    @Nullable
    private final String productKey;
    @Nullable
    private final String deviceId;

    private boolean subscribedMessageTopic;
    private boolean subscribedReplyTopic;
    //
    private long createTime;
    private long lastActivityTime;

    private Session(@NotNull String sessionId, @Nullable String productKey, @Nullable String deviceId) {
        this.sessionId = sessionId;
        this.productKey = productKey;
        this.deviceId = deviceId;
    }

    public void updateActiveTime(long lastActivityTime) {
        this.lastActivityTime = lastActivityTime;
    }

    public static Session create(SessionInfo sessionInfo) {
        Session session = new Session(sessionInfo.getSessionId(), sessionInfo.getProductKey(), sessionInfo.getDeviceId());
        session.createTime = System.currentTimeMillis();
        session.lastActivityTime = session.createTime;
        return session;
    }
}